package at.xtools.pwawrapper;

import at.xtools.pwawrapper.ui.UIManager;
import at.xtools.pwawrapper.webview.WebViewHelper;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;

public class MainActivity extends Ability {
    // Globals
    private UIManager uiManager;
    private WebViewHelper webViewHelper;
    private boolean intentHandled = false;

    @Override
    protected void onStart(Intent intent) {
        // Setup Theme
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_activity_main);

        // Setup Helpers
        uiManager = new UIManager(this);
        webViewHelper = new WebViewHelper(this, uiManager);

        // Setup App
        webViewHelper.setupWebView();
        uiManager.changeRecentAppsIcon();

        // Check for Intents
        try {
            // Handle URLs opened in Browser
            // Load up the Web App
            webViewHelper.loadHome();
        } catch (Exception e) {
            // Load up the Web App
            webViewHelper.loadHome();
        }
    }

    @Override
    protected void onInactive() {
        webViewHelper.onPause();
        super.onInactive();
    }

    @Override
    protected void onActive() {
        webViewHelper.onResume();
        // retrieve content from cache primarily if not connected
        webViewHelper.forceCacheIfOffline();
        super.onActive();
    }

    // Handle back-press in browser
    @Override
    public void onBackPressed() {
        if (!webViewHelper.goBack()) {
            super.onBackPressed();
        }
    }
}
